package com.cat.dynamicPlanning;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/delete-operation-for-two-strings/description/
 * @create 2025/9/3 18:40
 * @since JDK17
 */

public class Solution58 {
    public int minDistance(String w1, String w2) {
        int n = w1.length(), m = w2.length();
        int[][] dp = new int[n + 1][m + 1];
        for (int i = 1; i <= m; i++) {
            dp[0][i] = i;
        }
        for (int i = 1; i <= n; i++) {
            dp[i][0] = i;
        }

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                int min = Integer.MAX_VALUE;
                if (w1.charAt(i - 1) == w2.charAt(j - 1)) {
                    min = dp[i - 1][j - 1];
                }
                min = Math.min(min, dp[i - 1][j] + 1);
                min = Math.min(min, dp[i][j - 1] + 1);
                dp[i][j] = min;
            }
        }
        return dp[n][m];
    }
}
